/*
 * PPMC.cpp
 *
 *  Created on: 25/06/2012
 *      Author: natuchis
 */

#define MAX 5
#define TAMBLOQUE 256

#include "PPMC.h"

PPMC::PPMC(string ruta, char indicador) {

	//Archivo a descomprimir
	if (indicador == 'd') {
		this->arit = new CompresorAritmetico(ruta.c_str(),'d');
		this->ordenMax = this->arit->getOrdenCompresionMaximo();
	}
	//Archivo a comprimir
	else {
		if (indicador == 'c') {
			this->arit = new CompresorAritmetico(ruta.c_str(),'c');
			this->ordenMax = MAX;
			this->arit->setOrdenCompresionMaximo(this->ordenMax);
		}
		else {
			cout << "Parametro invalido. Para comprimir '-c'. Para descomprimir 'd'" << endl;
		}
	}

	this->admin = new AdminDeContextos(this->ordenMax);
	this->modelos = new Modelos(this->arit,this->admin);
	this->modelos->setOrdenMaximo(this->ordenMax);
	this->archAComp = new Archivo (ruta,TAMBLOQUE);
}

PPMC::~PPMC() {
	delete this->arit;
	delete this->modelos;
	delete this->admin;
	delete this->archAComp;
}

void PPMC::comprimir() {

	unsigned short int i = 0;

	char carac = this->archAComp->get();

	while (!this->archAComp->isEof()) {

			this->modelos->comprimir(this->admin->getContexto(i),carac,false);
			this->modelos->vaciarExcluidos();
			this->admin->agregarCaracterACtx(carac);
			i++;
			if (i > this->ordenMax) {
				i = this->ordenMax;
			}
			carac = this->archAComp->get();
	}
	this->modelos->comprimir(this->admin->getContexto(i),carac,true);
}

void PPMC::descomprimir() {

	this->modelos->descomprimir();
}
